package com.bl.org.controller;

import com.bl.basic.exception.BusinessException;
import com.bl.basic.util.ExcelUtil;
import com.bl.org.domain.ShopAuditLog;
import com.bl.org.dto.ShopDto;
import com.bl.org.service.IShopService;
import com.bl.org.domain.Shop;
import com.bl.org.query.ShopQuery;
import com.bl.basic.util.JsonResult;
import com.bl.basic.util.PageList;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids){
        try {
                shopService.patchDelete(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> loadAll(){
        return shopService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 店铺入驻接口
     */
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return JsonResult.me();

        } catch (BusinessException e) {
            // 业务异常
           e.printStackTrace();
           return JsonResult.me().setMessage("入驻失败"+e.getMessage());
        }catch (Exception e){
            // 其他错误500
            e.printStackTrace();
            return JsonResult.me().setMessage("系统错误，骚后重试");
        }


    }
    /**
     * 接口：店铺审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public JsonResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return new JsonResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"审核失败");
        }
    }


    /**
     * 接口：店铺审核驳回
     * @param
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public JsonResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new JsonResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"驳回失败");
        }
    }

    /**
     * 店铺激活
     * @param id
     * @param resp
     */
    @GetMapping("/active/{id}")
    public  void active(@PathVariable Long id, HttpServletResponse resp){
        try{
            shopService.active(id);
            resp.sendRedirect("http://localhost:8081/#/login");
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.loadAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 导入
     */
    @PostMapping("/fileImport")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,0,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");
        for (Shop shop:list) {
            System.out.println(shop);
        }
        //保存到数据库中[自己实现，提示：动态sql 批量插入]
    }
    /**
     *  echarts数据
     */
    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return shopService.getEcharts();
    }

}
